import {appState} from "@/store/app"
/**
 * 将数组转化成树结构
 */
export const arrayToTree = function (arr: any) {
  // 删除所有的children,以防止多次调用
  arr.forEach(function (item:any) {
    delete item.children;
  });
  let map: any = {}; //构建map
  arr.forEach((i:any) => {
    map[i.Id] = i; //构建以Id为键 当前数据为值
  });
  let treeData: any[] = [];
  arr.forEach((child:any) => {
    const mapItem = map[child.ParentId]; //判断当前数据的ParentId是否存在map中
    if (mapItem) {
      //存在则表示当前数据不是最顶层的数据
      //注意： 这里的map中的数据是引用了arr的它的指向还是arr,当mapItem改变时arr也会改变，踩坑点
      (mapItem.children || (mapItem.children = [])).push(child); //这里判断mapItem中是否存在child
    } else {
      //不存在则是顶层数据
      treeData.push(child);   
    }
  });
  return treeData;
}
/**
 * 循环遍历一个树结构
 */
export const mapTree = function (
  para: {
    /**
     * 表示用来放置子元素的key的name值
     */
    children: string;
  },
  treeObj: any,
  callback: (node: any, parentNode: any) => any
) {

  function fun(obj: any, parentObj: any) {
    if (parentObj && parentObj.Id) {
      obj.ParentId = parentObj.Id
    } else {
      obj.ParentId = 0
    }
    let objData = callback(obj, parentObj);
    if (typeof objData !== "undefined") {
      obj = objData;
    }
    let childList = obj[para.children];
    if (childList && childList.length > 0) {
      childList.map((item: any) => {
        fun(item, obj);
      });
    }
  }
  let paraData: any;
  if (Object.prototype.toString.call(treeObj) == "[object Array]") {
    //表示为数组的时候
    paraData = {
      [para.children]: treeObj
    };
  } else {
    paraData = treeObj;
  }

  fun(paraData, null);
};
/**
 * 服务器时间转为js Date对象实例
 */
export const dateTo = function (dateString: string) {
  if (!dateString) { return }
  if (typeof dateString == 'object') { return dateString }
  var regEx = new RegExp("\\-", "gi");
  dateString = dateString.replace(regEx, "/");
  //去掉最后的毫秒值,用于兼容苹果手机
  return new Date(dateString.split('.')[0]);
}
/**
 * 按钮权限
 */
 export const getPanelUxBtn= function (menuKey:string){
	let menuAction = appState.menuActionMap[menuKey];
	let btnUx = ['search']
	menuAction['add'] ? btnUx.push('add') : "";
	menuAction['delete'] ? btnUx.push('delete') : "";
	return btnUx
}
/**
 * 对象数组去重
 */
 export const objArraySet = function <T>(arr: T[], paraName: string): T[] {
  let objMap: any = {};
  let resultList: T[] = [];

  for (let item of arr) {
      if (!objMap[(item as any)[paraName]]) {
          objMap[(item as any)[paraName]] = true;
          resultList.push(item)
      }
  }

  return resultList
}
/**
 * objArror对象数组第一个出现属性等于prname的对象索引
 * objArror 也可以使用为对象
 */
 export const find = function (objArror: any, prNamn: string, value: any) {
  if (Array.isArray(objArror)) {
      // objArror为数组
      for (let i = objArror.length - 1; i >= 0; i--) {
          if (objArror[i][prNamn] == value) {
              return i
          }
      }
  } else {
      // objArror为对象
      for (let x in objArror) {
          if (objArror[x][prNamn] == value) {
              return x
          }
      }
  }
  return -1;
}


/**
 * 计算字符长度
 */
 export const countCharacterLen=function (str:string) {
  let len = 0;
  for (let i = 0; i < str.length; i++) {
    if (str.charCodeAt(i) > 127 || str.charCodeAt(i) == 94) {
      len += 2;
    } else {
      len++;
    }
  }
  return len;
 }